import jwt, { JwtPayload } from "jsonwebtoken"
import 'dotenv/config';
/**
 * 生成jwt凭据
 */
export function signJwtToken(username:string) {
    return jwt.sign({
        username
    },process.env.JWT_SECRET!,{
        expiresIn: (60 * 60 * 24) * 7 // 7天有效期
    })
}

/**
 * 验证jwt凭据
 * @param token 凭据
 * @returns 用户名
 */
export function verifyJwtToken(token:string) : string {
    const payload = jwt.verify(token,process.env.JWT_SECRET!) as JwtPayload
    return payload.username;
}